<?php

class QStorage
{
    private $QXC;
    public $GUID = 0;
    private $dataArray = array();
    private $objType;
    private $objName;

    function __construct($qxc)
    {
        $this->QXC = $qxc;
    }

    public function Load($name, $cond)
    {
        $this->QXC->Db->Select('GUID')
            ->From('qxc_storage');

        $this->objName = $name;

        if(is_array($cond))
        {
            foreach ($cond as $val)
            {
                $m = preg_split("/[\s]*(=|!=)[\s]*/", $val);

                $this->QXC->Db->Where("Name='{$this->objName}' AND DataKey='{$m[0]}' AND DataValue='{$m[1]}'");
            }
        }
        else
        {
            $m = preg_split("/[\s]*(=|!=)[\s]*/", $val);

            $this->QXC->Db->Where("Name='{$this->objName}' AND DataKey='{$m[0]}' AND DataValue='{$m[1]}'");
        }

        $q = $this->QXC->Db->Query();

        if(!$q)
        {
            return false;
        }

        foreach ($q as $val)
        {
            $this->GetByGuid($val['GUID']);
        }

        $this->setProperties(current($this->dataArray));

        return true;
    }

    public function LoadGUID($guid)
    {
        $this->GetByGuid($guid);
        
        $this->setProperties(current($this->dataArray));

        return true;
    }

    public function Save($name = null)
    {
        if(!is_null($name))
        {
            $this->objName = $name;
        }
                
        if(!$this->GUID)
        {            
            $this->QXC->Db->Query("INSERT INTO qxc_storage_guid (Timestamp) VALUES (:timestamp)",
                array('timestamp'=>time()));

            $this->GUID = $this->QXC->Db->LastId();

            reset($this->dataArray);

            foreach (current($this->dataArray) as $key=>$val)
            {
                $this->QXC->Db->Query("INSERT INTO qxc_storage (GUID, Name, DataKey, DataValue) VALUES ('{$this->GUID}', '{$this->objName}', '{$key}', '{$val}')");
            }
        }
        else
        {            
            foreach (current($this->dataArray) as $key=>$val)
            {
                $this->QXC->Db->Query("UPDATE qxc_storage SET DataValue=:VALUE WHERE GUID=:GUID AND DataKey=:KEY",
                    array('GUID' => $this->GUID,
                            'KEY' => $key,
                            'VALUE' => $val));
            }
        }
        
        return true;
    }
    
    public function Count()
    {
        return count($this->dataArray);
    }    

    public function Next()
    {
        return $this->setProperties(next($this->dataArray));
    }

    public function Prev()
    {
        return $this->setProperties(prev($this->dataArray));
    }

    public function GetGuid()
    {
        return  $this->GUID;
    }

    private function GetByGuid($guid)
    {
        $this->QXC->Db->SetVar('GUID', (int)$guid);

        $this->QXC->Db->Select(array('DataKey', 'DataValue'))
            ->From('qxc_storage')
            ->Where('GUID=:GUID');

        $q = $this->QXC->Db->Query();

        foreach ($q as $val)
        {
            $this->dataArray[$guid][$val['DataKey']] = $val['DataValue'];
        }
    }

    public function Delete($guid)
    {
        $this->QXC->Db->Set('GUID', (int)$guid);
        
        $this->QXC->Db->Delete()
            ->From('qxc_storage')
            ->Where('GUID=:GUID');

        $this->QXC->Db->Query();

        return true;
    }

    public function ToArray()
    {
        return $this->dataArray;
    }

    private function setProperties($arr)
    {
        if(!$arr) return false;

        $this->GUID = key($this->dataArray);
        
        foreach ((array)$arr as $key=>$val)
        {
            $this->{$key} = $val;
        }

        return true;
    }

    function __set($name, $value)
    {
        $this->dataArray[$this->GUID][$name] = $value;
//        $this->{$name} = $value;
    }

    function __get($name)
    {
        $arr = current($this->dataArray);

        return $arr[$name];
    }
}

